package com.frikinet.lifeguard.util;

import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.DateTool;

public class VelocityUtil
{
	@SuppressWarnings("rawtypes")
	public static String getDocument(String template, Map values)
	{
		try
		{
			Velocity.init();
			VelocityContext vc = new VelocityContext(values);
			vc.put("dateTool", new DateTool());
			Reader reader = new InputStreamReader(VelocityUtil.class.getResourceAsStream(String.format("/com/frikinet/lifeguard/templates/%s.vm", template)));
			StringWriter writer = new StringWriter();
			Velocity.evaluate(vc, writer, "vlog", reader);
			writer.close();
			reader.close();
			return writer.toString();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
